package com.test.controller;


import com.test.bean.*;
import com.test.service.UserService;
import com.test.utils.ImportExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("daily")
public class DailyController {

    @Autowired
    private UserService userService;

//    学生选课信息单条录入
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddCourse(Stu_Course stu_course, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = stu_course.getId();
        Student dstudent = userService.queryStudent(s_id);
        int c_id = stu_course.getCourse_id();
        Course  dcourse = userService.queryCourse(c_id);
        if(dstudent != null && dcourse != null){
            try {
                userService.addCourse(stu_course);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    学生选课信息批量录入
    @ResponseBody
    @RequestMapping(value = "adds",method = RequestMethod.POST)
    public class LeadingExcelController {

    /**
     * 描述：通过传统方式form表单提交方式导入excel文件
     *
     * @param request
     * @throws Exception
     */
        @RequestMapping("/form")
        public String form(HttpServletRequest request) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            InputStream in = null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");

            if (file.isEmpty()) {
                throw new Exception("文件不存在！");
            }
            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
            in.close();

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Stu_Course stu_course = new Stu_Course();
                stu_course.setCourse_id(Integer.parseInt(String.valueOf(lo.get(0))));
                stu_course.setId(Integer.parseInt(String.valueOf(lo.get(1))));
                stu_course.setCourse_term(String.valueOf(lo.get(2)));
                stu_course.setCourese_score(String.valueOf(lo.get(3)));

            }

            return null;
        }

        /**
         * 描述：通过 jquery.form.js 插件提供的ajax方式上传文件
         *
         * @param request
         * @param response
         * @throws Exception
         */
        @ResponseBody
        @RequestMapping(value = "ajaxUpload.do", method = {RequestMethod.GET, RequestMethod.POST})
        public Result ajaxUploadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            Result result = new Result();
            //    System.out.println("通过 jquery.form.js 提供的ajax方式上传文件！");

            InputStream in = null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");
            if (file.isEmpty()) {
                result.setCode(400);
                result.setMessage("excel解析录入失败");
            }

            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Stu_Course stu_course = new Stu_Course();
                stu_course.setCourse_id(Integer.parseInt(String.valueOf(lo.get(0))));
                stu_course.setId(Integer.parseInt(String.valueOf(lo.get(1))));
                stu_course.setCourse_term(String.valueOf(lo.get(2)));
                stu_course.setCourese_score(String.valueOf(lo.get(3)));

                //  System.out.println("打印信息-->机构:"+vo.getCode()+"  名称："+vo.getName()+"   时间："+vo.getDate()+"   资产："+vo.getMoney());
            }

            result.setCode(200);
            result.setMessage("excel解析录入成功");
            PrintWriter out = null;
            response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
            out = response.getWriter();
            //            out.print("文件导入成功！");
            out.flush();
            out.close();
            return result;
        }
    }

//    谈心谈话录入
    @ResponseBody
    @RequestMapping(value = "talk",method = RequestMethod.POST)
    public Result AddTalk(Daily_Talk talk, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = talk.getId();
        Student dstudent = userService.queryStudent(s_id);
        if (dstudent != null){
            try {
                userService.addTalk(talk);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }

        return result;
    }

//    谈心谈话查询
    @ResponseBody
    @RequestMapping(value = "talk_query",method = RequestMethod.GET)
    public List<Daily_Talk> QueryStudents(int id) {
        Result result = new Result();
        List<Daily_Talk> list = new ArrayList<Daily_Talk>();
        try {
            list = userService.queryTalk(id);
            result.setMessage("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return list;
    }

//    查课情况录入
    @ResponseBody
    @RequestMapping(value = "check",method = RequestMethod.POST)
    public Result AddCheck(Daily_Check check, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = check.getId();
        Student dstudent = userService.queryStudent(s_id);
        int c_id = check.getC_id();
        Course  dcourse = userService.queryCourse(c_id);
        if(dstudent != null && dcourse != null){
            try {
                userService.addCheck(check);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    违纪情况录入
    @ResponseBody
    @RequestMapping(value = "discipline",method = RequestMethod.POST)
    public Result AddDiscipline(Daily_Discipline daily_discipline, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = daily_discipline.getId();
        Student dstudent = userService.queryStudent(s_id);
        if(dstudent != null ){
            try {
                userService.addDiscipline(daily_discipline);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    违纪情况照片录入
    @ResponseBody
    @RequestMapping(value = "image",method = RequestMethod.POST)
    public Result ImageReward(HttpServletRequest request, MultipartFile[] files) throws Exception {
        // 判断文件是否成功上传
        Result result = new Result();
        // 文件存储路径
        String path = request.getSession().getServletContext().getRealPath("/image/");
        // 使用HashMap键值对存储上传的文件
        Map<String,Object> map=new HashMap<>();
        try{
            if(files!=null && files.length>0){
                for(int i=0;i<files.length;i++) {
                    if (files[i] != null) {
                        // 获取上传的文件名
                        String fileName = files[i].getOriginalFilename();
                        // 在指定路径中创建一个文件(该文件是空的)
                        File file = new File(path + fileName);
                        // 将上传的文件写入指定路径
                        files[i].transferTo(file);
                        map.put(fileName,fileName);
                    }
                    request.setAttribute("map",map);
                    result.setCode(200);
                    result.setMessage("上传成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("上传失败");
            result.setCode(400);//异常
        }
        return result;

    }


//    日常行为查询
    @ResponseBody
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public List<Daily_Bad> queryDailyBad(int id,String name){
        Result result = new Result();
        List<Daily_Bad> list = new ArrayList<Daily_Bad>();
        try {
            list = userService.queryDailybad(id);
            result.setMessage("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return list;
    }


//    查寝情况录入
    @ResponseBody
    @RequestMapping(value = "dormissue_add",method = RequestMethod.POST)
    public Result AddDormissue(Daily_Dormissue daily_dormissue, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = daily_dormissue.getId();
        Student dstudent = userService.queryStudent(s_id);
        if(dstudent != null ){
            try {
                userService.addDormissue(daily_dormissue);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    特殊学生录入
    @ResponseBody
    @RequestMapping(value = "dormissue_add",method = RequestMethod.POST)
    public Result AddEspecial(Daily_especial daily_especial, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        int s_id = daily_especial.getId();
        Student dstudent = userService.queryStudent(s_id);
        if(dstudent != null ){
            try {
                userService.addEspecial(daily_especial);
                result.setMessage("录入成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("录入失败");
                result.setCode(400);//异常
            }
        }
        else{
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    特殊学生更新
    @ResponseBody
    @RequestMapping(value = "especial_update",method = RequestMethod.PUT)
    public Result UpdateEspecial(Daily_especial daily_especial,HttpSession session){
        Result result = new Result();
        int id = daily_especial.getId();
        Student dstudent = userService.queryStudent(id);
        if(dstudent != null){
            try {
                userService.updateEspecial(daily_especial);
                result.setMessage("修改成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();//在日志中输出错误
                result.setMessage("修改失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("修改失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    特殊学生删除
    @ResponseBody
    @RequestMapping(value = "especial_delete",method = RequestMethod.DELETE)
    public Result DeleteEspecial(Daily_especial daily_especial,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        int id = daily_especial.getId();
        Student student1 = userService.queryStudent(id);
        if(student1 != null){
            try {
                userService.deleteEspecial(daily_especial);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }

}
